Thread: Why we use "->" in this code? [question about an example in "Teach yourself C++" book

  1. #1
    Registered User rayan's Avatar
    Join Date
    May 2016
    Location
    Everywhere that is C++
    Posts
    1

    Question Why we use "->" in this code? [question about an example in "Teach yourself C++" book

    Hi fellas
    I'm reading "Teach yourself C++" book by Herbert Schildt to learn C++. there is an example in chapter 10 which is tough to me. this is the code I'm talking about:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    
    using namespace std;
    
    class list {
    public:
        list *head; // pointer to start of list
        list *tail; //pointer to end of list
        list *next; //pointer to next item
        int num; // value to be stored
        
        list() { head = tail = next = null;}
        virtual void store(int i) = 0;
        virtual void retrieve() = 0;
    };
    
    //create a queue-type list
    class queue : public list {
    public:
        void store(int i);
        int retrieve();
    };
    
    void queue::store(int i)
    {
        list *item;
        
        item = new queue;
        if(!item) {
            cout << "Allocation error.\n";
            exit(1);
        }
        item->num = i;
        
        //put on end of list
        if(tail) tail->next = item;
        tail = item;
        item->next = null;
        if(!head) head = tail;
    }
    
    int queue::retrieve()
    {
        int i;
        list *p;
        
        if(!head) {
            cout << "list empty.\n";
            return 0;
        }
        // remove from start of list
        i = head->num;
        p = head;
        head = head->next;
        delete p;
        
        return i;
    }
    
    // create a stack-type list
    class stack : public list {
    public:
        void store(int i);
        int retrieve();
    };
    
    void stack::store(int i)
    {
        list *item;
        
        item = new stack;
        if(!item) {
            cout << "Allocation error.\n";
            exit(1);
        }
        item->num = i;
        
        //put on front of list for stack-like operation
        if(head) item->next = head;
        head = item;
        if(!tail) tail = head; 
    }
    
    int stack::retrieve()
    {
        int i;
        list *p;
        
        if(!head) {
            cout << "list empty.\n";
            return 0;
        }
        // remove from start of list
        i = head->num;
        p = head;
        head = head->next;
        delete p;
        
        return i;
    }
    
    int main()
    {
        list *p;
        
        // demonstrate queue
        queue q_ob;
        p = &q_ob; // point to queue
        
        p->store(1);
        p->store(2);
        p->store(3);
        
        cout << "Queue: ";
        cout << p->retrieve();
        cout << p->retrieve();
        cout << p->retrieve();
        
        cout << '\n';
        
        //demonstrate stack
        stack s_ob;
        p = &s_ob; // point to stack
        
        p->store(1);
        p->store(2);
        p->store(3);
        
        cout << "Stack: ";
        cout << p->retrieve();
        cout << p->retrieve();
        cout << p->retrieve();
        
        cout << '\n';
        
        return 0;
    }
    This is alone tough code that I've seen so far in this book. now the questions:

    1- He has defined three class pointer named head,tail and next as "list" object. are we allowed to define these pointers within class "list"? it a bit made me confused.

    2- In some parts of the code you can see something e.g. this:

    "tail->next = item;"

    We use "->" to refer a data-object of a class. am I right? but here "tail" and "next" are both pointer to an object of "list". then why does he use "->"? totally these parts e.g. "tail->next" make me confused.

    3- Would you please tell me how "store" and "retrive" functions work?

    Notice this part of the book talks about polymorphism by virtual functions.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rayan
    We use "->" to refer a data-object of a class. am I right?
    To access a member variable y of an object x of class type, you would write x.y. If you have a pointer p that points to x, you could write (*p).x, but for convenience you can use p->x instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-21-2016, 12:35 AM
  2. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  3. Replies: 1
    Last Post: 11-16-2007, 08:31 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread